home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 1999 #2 / Amiga Plus CD - 1999 - No. 2.iso / System-Boost / Workbench / ToolManager / Source / Prefs / entries.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  6KB  |  202 lines

  1. /*
  2.  * entries.c  V3.1
  3.  *
  4.  * TM dock entries handling routines
  5.  *
  6.  * Copyright (C) 1990-98 Stefan Becker
  7.  *
  8.  * This source code is for educational purposes only. You may study it
  9.  * and copy ideas or algorithms from it for your own projects. It is
  10.  * not allowed to use any of the source codes (in full or in parts)
  11.  * in other programs. Especially it is not allowed to create variants
  12.  * of ToolManager or ToolManager-like programs from this source code.
  13.  *
  14.  */
  15.  
  16. #include "toolmanager.h"
  17.  
  18. /* Read dock entries from configuration file */
  19. #undef  DEBUGFUNCTION
  20. #define DEBUGFUNCTION ReadDockEntries
  21. void ReadDockEntries(struct IFFHandle *iffh, struct MinList *list,
  22.                      Object *obj, Object **lists)
  23. {
  24.  struct CollectionItem *ci;
  25.  
  26.  /* Any entries found? */
  27.  if (ci = FindCollection(iffh, ID_TMDO, ID_ENTR)) {
  28.   struct DockEntry *de;
  29.  
  30.   ENTRIES_LOG(LOG1(Collection, "0x%08lx", ci))
  31.  
  32.   /* Scan collection item list */
  33.   while (ci) {
  34.  
  35.    ENTRIES_LOG(LOG1(Next, "0x%08lx", ci))
  36.  
  37.    /* Allocate memory for next entry */
  38.    if (de = GetMemory(sizeof(struct DockEntry))) {
  39.     struct DockEntryChunk *dec = (struct DockEntryChunk *) ci->ci_Data;
  40.  
  41.     /* Attach objects */
  42.     de->de_Exec  = AttachObject(lists[TMOBJTYPE_EXEC],  obj,
  43.                                 dec->dec_ExecObject);
  44.     de->de_Image = AttachObject(lists[TMOBJTYPE_IMAGE], obj,
  45.                                 dec->dec_ImageObject);
  46.     de->de_Sound = AttachObject(lists[TMOBJTYPE_SOUND], obj,
  47.                                 dec->dec_SoundObject);
  48.  
  49.     /* Insert entry at the head of the list */
  50.     AddHead((struct List *) list, (struct Node *) de);
  51.  
  52.    } else
  53.  
  54.     /* No memory, leave loop */
  55.     break;
  56.  
  57.    /* Next collection item */
  58.    ci = ci->ci_Next;
  59.   }
  60.  }
  61. }
  62.  
  63. /* Write dock entries to configuration file */
  64. #undef  DEBUGFUNCTION
  65. #define DEBUGFUNCTION WriteDockEntries
  66. BOOL WriteDockEntries(struct IFFHandle *iffh, struct MinList *list)
  67. {
  68.  struct DockEntry      *de  = (struct DockEntry *) GetHead(list);
  69.  BOOL                   rc  = TRUE;
  70.  struct DockEntryChunk  dec;
  71.  
  72.  /* For each entry in the list */
  73.  while (de) {
  74.  
  75.   /* Use object addresses as IDs */
  76.   dec.dec_ExecObject  = (ULONG) (de->de_Exec  ? de->de_Exec->ad_Object  :
  77.                                  NULL);
  78.   dec.dec_ImageObject = (ULONG) (de->de_Image ? de->de_Image->ad_Object :
  79.                                  NULL);
  80.   dec.dec_SoundObject = (ULONG) (de->de_Sound ? de->de_Sound->ad_Object :
  81.                                  NULL);
  82.  
  83.   /* Write entry */
  84.   if ((rc = WriteProperty(iffh, ID_ENTR, &dec, sizeof(struct DockEntryChunk)))
  85.        == FALSE)
  86.  
  87.    /* Error, leave loop */
  88.    break;
  89.  
  90.   /* Next entry */
  91.   de = (struct DockEntry *) GetSucc((struct MinNode *) de);
  92.  }
  93.  
  94.  ENTRIES_LOG(LOG1(Result, "%ld", rc))
  95.  
  96.  return(rc);
  97. }
  98.  
  99. /* Free one dock entry */
  100. #undef  DEBUGFUNCTION
  101. #define DEBUGFUNCTION FreeDockEntry
  102. void FreeDockEntry(struct DockEntry *de)
  103. {
  104.  ENTRIES_LOG(LOG1(Entry, "0x%08lx", de))
  105.  
  106.  /* Detach objects */
  107.  if (de->de_Exec)  DoMethod(de->de_Exec->ad_Object,  TMM_Detach, de->de_Exec);
  108.  if (de->de_Image) DoMethod(de->de_Image->ad_Object, TMM_Detach, de->de_Image);
  109.  if (de->de_Sound) DoMethod(de->de_Sound->ad_Object, TMM_Detach, de->de_Sound);
  110.  
  111.  /* Free entry */
  112.  FreeMemory(de, sizeof(struct DockEntry));
  113. }
  114.  
  115. /* Free a list of dock entries */
  116. #undef  DEBUGFUNCTION
  117. #define DEBUGFUNCTION FreeDockEntries
  118. void FreeDockEntries(struct MinList *list)
  119. {
  120.  struct DockEntry *de;
  121.  
  122.  /* For each entry in the list */
  123.  while (de = (struct DockEntry *) RemTail((struct List *) list)) {
  124.  
  125.   ENTRIES_LOG(LOG1(Next entry, "0x%08lx", de))
  126.  
  127.   /* Free entry */
  128.   FreeDockEntry(de);
  129.  }
  130.  
  131.  ENTRIES_LOG(LOG0(Exit))
  132. }
  133.  
  134. /* Copy one dock entries */
  135. #undef  DEBUGFUNCTION
  136. #define DEBUGFUNCTION CopyDockEntry
  137. struct DockEntry *CopyDockEntry(struct DockEntry *de, Object *obj)
  138. {
  139.  struct DockEntry *rc;
  140.  
  141.  ENTRIES_LOG(LOG1(Entry, "0x%08lx", de))
  142.  
  143.  /* Allocate memory for new entry */
  144.  if (rc = GetMemory(sizeof(struct DockEntry))) {
  145.  
  146.   ENTRIES_LOG(LOG1(New entry, "0x%08lx", rc))
  147.  
  148.   /* Initialize entry */
  149.   rc->de_Exec  = NULL;
  150.   rc->de_Image = NULL;
  151.   rc->de_Sound = NULL;
  152.  
  153.   /* Attach objects */
  154.   if (((de->de_Exec  != NULL) &&
  155.        ((rc->de_Exec  = (struct AttachData *)
  156.                          DoMethod(de->de_Exec->ad_Object,   TMM_Attach, obj))
  157.          == NULL)) ||
  158.       ((de->de_Image != NULL) &&
  159.        ((rc->de_Image = (struct AttachData *)
  160.                          DoMethod(de->de_Image->ad_Object,  TMM_Attach, obj))
  161.          == NULL)) ||
  162.       ((de->de_Sound != NULL) &&
  163.        ((rc->de_Sound = (struct AttachData *)
  164.                          DoMethod(de->de_Sound->ad_Object,  TMM_Attach, obj))
  165.          == NULL))) {
  166.  
  167.    /* Could not attach objects */
  168.    ENTRIES_LOG(LOG0(Could not duplicate entry))
  169.  
  170.    /* Delete new entry */
  171.    FreeDockEntry(rc);
  172.  
  173.    /* Clear pointer */
  174.    rc = NULL;
  175.   }
  176.  }
  177.  
  178.  ENTRIES_LOG(LOG1(Result, "0x%08lx", rc))
  179.  
  180.  return(rc);
  181. }
  182.  
  183. /* Clear one attached object from a list of dock entries */
  184. #undef  DEBUGFUNCTION
  185. #define DEBUGFUNCTION RemoveDockEntryAttach
  186. BOOL RemoveDockEntryAttach(struct DockEntry *de, struct AttachData *ad)
  187. {
  188.  BOOL rc = TRUE;
  189.  
  190.  ENTRIES_LOG(LOG2(Arguments, "Entry 0x%08lx Attach 0x%08lx", de, ad))
  191.  
  192.  /* Object found? Yes, clear pointer and leave loop */
  193.  if      (ad == de->de_Exec)  de->de_Exec  = NULL;
  194.  else if (ad == de->de_Image) de->de_Image = NULL;
  195.  else if (ad == de->de_Sound) de->de_Sound = NULL;
  196.  else                         rc           = FALSE;
  197.  
  198.  ENTRIES_LOG(LOG1(Result, "%ld", rc))
  199.  
  200.  return(rc);
  201. }
  202.